﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Sylvain.Manager;
using System.IO;
using Sylvain.Model;

public partial class Admin_EditCurrentPublications : System.Web.UI.Page
{
    ISylvainManager objManger = new SylvainManager();
    static int PageId;
    /// <summary>
    /// Display content of the Page.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        if (!Page.User.Identity.IsAuthenticated)
            Response.Redirect("~/Admin/AdminLogin.aspx", false);

        if (!IsPostBack)
        {
            PageId = objManger.GetPageId("current_publications");
            FckEditor1.Value = objManger.GetPageContent(PageId);
            Publication objPublication = objManger.GetCurrentPublication();
            if (objPublication != null)
            {
                FckEditor1.Value = objPublication.PageDescription;
                this.txtTitle.Text = objPublication.Title;
                if (File.Exists(Server.MapPath("~/Publications/" + objPublication.PubFileName)))
                {
                    this.hlnkDownload.Visible = true;
                    this.hlnkDownload.NavigateUrl = "~/Publications/" + objPublication.PubFileName;
                }
            }
        }

    }
    /// <summary>
    /// Insert/Update content of the Page.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpdate_Click(object sender, EventArgs e)
    {        
        ISylvainManager objManger = new SylvainManager();
        //Get current user
        MembershipUser userInfo = Membership.GetUser();
        if (userInfo != null)
        {
            objManger.InsertPageContent(PageId, FckEditor1.Value, userInfo.UserName);

            // Save Publication
            Publication objUpdatePub = new Publication();
            objUpdatePub.PageDescription = this.FckEditor1.Value;
            objUpdatePub.Title = this.txtTitle.Text;
            string filename = "";
            if (fileuploadPdf.HasFile)
            {
                filename = Guid.NewGuid() + Path.GetExtension(fileuploadPdf.FileName);
                string imagePath = Server.MapPath("~/Publications/");
                fileuploadPdf.SaveAs(imagePath + filename);
            }
            Publication objPublication = objManger.GetCurrentPublication();
            

            objUpdatePub.PubFileName = filename;
            
            objUpdatePub.DateCreated = System.DateTime.Now;
            objUpdatePub.Status = true;            
            objUpdatePub.PageMasterId = PageId;
            if (objPublication != null && objPublication.Title != null && Convert.ToString(objUpdatePub.Title).ToLower().Trim().Equals(objPublication.Title.ToLower().Trim()))
            {
                if (filename == "")
                {
                    if (objPublication != null)
                    {
                        filename = objPublication.PubFileName;
                    }
                }
                objPublication.PubFileName = filename;
                objPublication.PageDescription = this.FckEditor1.Value;
                objManger.EditPublication(objPublication);
            }
            else
            {
                objManger.AddPublication(objUpdatePub);
            }
            if (File.Exists(Server.MapPath("~/Publications/" + filename)))
            {
                this.hlnkDownload.Visible = true;
                this.hlnkDownload.NavigateUrl = "~/Publications/" + filename;
            }
        }
        else
        {
            Response.Redirect("~/Admin/AdminLogin.aspx", false);
        }

        lblError.Text = "Content has been updated sucessfully.";
    }
}